﻿using DADAL;

namespace DABLL
{
    public class FormPoint
    {

        /// <summary> 添加界面点位
        /// 
        /// </summary>
        /// <param name="obj"></param>
        public static Response AddFormPoint(FormPointModel obj)
        {
            Response response = Response.DefaultResponse();
            bool res = FormPointModel.RecordExistenceWithName(obj.Name);
            if (res)
            {
                response.Msg = string.Format("【{0}】点位已存在", obj.Name);
                return response;
            }
            res = DevicePointModel.RecordExistenceWithNumber(obj.DevicePointNumber);
            if (!res)
            {
                response.Msg = string.Format("【{0}】设备点位不存在", obj.DevicePointName);
                return response;
            }

            List<FormPointModel> list = new List<FormPointModel>();
            list.Add(obj);
            int result = SqlSugarHelper.Insertable(list);
            if (result > 0)
            {
                response.Code = 200;
                response.Msg = string.Format("已添加【{0}】界面点位", obj.Name);
            }
            else
            {
                response.Msg = string.Format("界面点位添加失败");
            }
            return response;
        }

        /// <summary>
        /// 删除界面点位
        /// </summary>
        /// <param name="number">点位编号</param>
        /// <returns></returns>
        public static Response DeleteFormPoint(int number)
        {
            Response response = Response.DefaultResponse();
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["number"] = number;
            List<FormPointModel> list = SqlSugarHelper.Inquireable<FormPointModel>(where);
            if (list.Count > 0)
            {
                int deleteRes = SqlSugarHelper.Deleteable(list);
                if (deleteRes > 0)
                {
                    response.Code = 200;
                    response.Msg = string.Format("界面点位已删除");
                }
                else
                {
                    response.Msg = string.Format("界面点位删除失败");
                }
                return response;
            }
            else
            {
                response.Msg = string.Format("【{0}】界面点位不存在，无法删除", number);
            }
            return response;
        }

        /// <summary>
        /// 界面点位列表
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns></returns>
        public static List<FormPointModel> FormPointList(int page, int pageSize, ref int totalCount)
        {
            List<FormPointModel> list = SqlSugarHelper.InquireableToPageList<FormPointModel>(null, page, pageSize, ref totalCount);
            return list;
        }

    }
}
